use super::*;
use std::io;

pub struct DownloadCmd<'a> {
  force: bool,
  cos_path: &'a str,
  local_path: &'a str,
}

impl<'a> DownloadCmd<'a> {
  pub fn new(force: bool, cos_path: &'a str, local_path: &'a str) -> Self {
    DownloadCmd {
      force,
      cos_path,
      local_path,
    }
  }
}

impl<'a> BinaryCommand for DownloadCmd<'a> {
  type Output = io::Result<bool>;
  fn exec(&self) -> Self::Output {
    if cfg!(target_os = "windows") {
      panic!("no impl for windows!")
    } else {
      let args = if !self.force {
        // 本地遇到同名文件跳过
        vec!["download", "-s", self.cos_path, self.local_path]
      } else {
        // 本地遇到同名文件也下载并覆盖
        vec!["download", "-f", self.cos_path, self.local_path]
      };
      Command::new(COS_BIN).args(args).spawn()?.wait()?;
      Ok(true)
    }
  }
}
